<?php
defined('_JEXEC') or die( 'Restricted access' );
class JElementSlideshow extends JElement
{
		
	function fetchElement($name, $value, &$node, $control_name)
	{
		global $mainframe;
		if (!$mainframe->isAdmin()) {
			return;
		}
		
		JHTML::_('behavior.mootools');
		JHTML::_('behavior.modal');
		
		jimport('joomla.filesystem.file');
		$document=&JFactory::getDocument();
		$document->addScriptDeclaration("
		var images;
		window.addEvent('load',function(){
			
			images=$$('#img-choser img');
			
			$('Remove').addEvent('click',function(){
			
				$('paramsslides').getChildren().each(function(el){
					
					if(el.selected == true){
						el.remove();
					}
					
					if(el.selected == false){
						el.selected=true;
					}
				
				})
				
			
			});
			
			$('SelectAll').addEvent('click',function(){
			
				$('paramsslides').getChildren().each(function(el){
					
					if(el.selected == false){
						el.selected =true;
					}
					
					
				
				})
				
			
			});
			
			$('OrderUp').addEvent('click',function(){
			
				var select=$('paramsslides'); 
				var cildren=select.getChildren();
				cildren.each(function(el,index){
				
					if(el.selected == true){
						
						if((index-1) >= 0){
							
							var tmp=cildren[index-1].value;
							cildren[index-1].value=el.value;
							cildren[index-1].innerHTML=el.value;
							el.value=tmp;
							el.innerHTML=tmp;
							
						}
					
					}
				
				});
			
			});
			$('OrderDown').addEvent('click',function(){
			
				var select=$('paramsslides'); 
				var cildren=select.getChildren();
				cildren.each(function(el,index){
				
					if(el.selected == true){
						
						if((index+1) < cildren.length){
							
							var tmp=cildren[index+1].value;
							cildren[index+1].value=el.value;
							cildren[index+1].innerHTML=el.value;
							el.value=tmp;
							el.innerHTML=tmp;
							
						}
					
					}
				
				});
			
			});
			
			
		});
		
		function addThis(el){
		
			var name=images[el].name;
			var select=$('paramsslides');
			var newoption = new Option(\"\"+name+\"\", \"\"+name+\"\");
			try
			{
				newoption.setProperty('selected', 'selected');
				select.add(newoption, null);
			}
			catch (err)
			{
				newoption.selected=true;
				select.add(newoption);
				
			}

		}

		");
		$document->addStyleDeclaration(".paramlist_value #img-choser{display:none;}");
		$files=JFolder::files(JPATH_SITE.DS.'templates'.DS.'braincode-grunge'.DS.'slides');
		$return='<div id="img-choser">';
		if(!empty($files)){
		$list=array();
			
			$i=0;
			foreach($files as $file){
				$ext=JFile::getExt($file);
				if(($ext=='jpg'||$ext=='jpeg'||$ext=='png'||$ext=='gif')){
					$return.='<div style="padding:5px;margin:10px;float:left;border:1px solid #000"><a href="javascript:void(0);"  onclick="parent.addThis(\''.$i.'\');parent.SqueezeBox.close();"><img title="Click to add" style="width:110px;height:80px;display:block" src="'.JURI::root().'templates/braincode-grunge/slides/'.JFile::getName($file).'" name="'.JFile::getName($file).'" /></a><br /> <span>'.JFile::getName($file).'</span></div>';
					$i++;
				}
			}
		
		}
		$return.='</div>';
		$mitems=array();
		$i=0;
		if(!empty($value)){
			
			if(is_array($value)){
		
				foreach($value as $val){
					$mitems[$i]->value=$val;
					$mitems[$i]->name=$val;
					$i++;
				}
		
			}else{
				$mitems[$i]->value=$value;
				$mitems[$i]->name=$value;
			}
		}
		
				$output= JHTML::_('select.genericlist',  $mitems, ''.$control_name.'['.$name.'][]', 'class="inputbox" style="width:100%"   multiple="multiple" size="10"', 'value', 'value', $value );

		return $output.'<a href="#img-choser" class="modal"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/add.png"  alt="'.JText::_('ADD SLIDE').'" title="'.JText::_('ADD SLIDE').'"></a>&nbsp;
		<a href="javascript:void(0)" id="SelectAll"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/all.png"  alt="'.JText::_('SELECT ALL').'" title="'.JText::_('SELECT ALL').'"></a>&nbsp;<a href="javascript:void(0)" id="Remove"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/delete.png" alt="'.JText::_('DELETE SELECTED').'" title="'.JText::_('DELETE SELECTED').'"></a>
		&nbsp;<a href="javascript:void(0)" id="OrderUp"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/up.png"  alt="'.JText::_('MOVE UP').'" title="'.JText::_('MOVE UP').'"></a>
		&nbsp;<a href="javascript:void(0)" id="OrderDown"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/down.png"  alt="'.JText::_('MOVE UP').'" title="'.JText::_('MOVE UP').' slide"/></a><p>'.JText::_('STORED').'</p>'.$return;
		
	}
	
}	?>

